<?php

include 'atom.php';

$query_string= "/MINI";
function AppendNodeNoValue($query_string, $node_name)
{
	$Ret_Array = OpenXmlFileForWrite(XMLFILE);
	$doc = $Ret_Array["doc"];
	$xpath = $Ret_Array["xpath"];

	$results = $xpath->evaluate($query_string);
	$result = $results->item(0);
	
	$new_node = $doc->createElement($node_name);
	
	$result->appendChild($new_node);

	$doc->save(XMLFILE);
}
#AppendNodeNoValue($query_string, "TEST");

$query_string ="/MINI/NETWORK/DNSS/DNS[last()]" ;
function Judge_Xpath($query_string)
{	
	$array = explode('/',$query_string, -1);
        $pos = strrpos($query_string, '/') ;
        $xpath_string = substr($query_string, 0, $pos);

	$pos = strrpos($query_string, '/') ;
	$xpath_string = substr($query_string, 0, $pos);

	$xpath = OpenXmlFileForRead(XMLFILE);
	
	for ($k = 0; $k < count($array); $k ++ )
	{
		print "Number:".$k . "\n";
		print "Array :".$array[$k]. "\n";
		$string = $string . $array[$k];
		print "String:".$string . "\n";
		

		if ($k == 0)
		{
			$string = "/";
		}
		{
			$results = $xpath->query($string);
			$flag = 0;
			foreach($results as $result)
			{
				print "array K + 1:" . $array[$k +1] . "\n";
				print "relult: " . $result->nodeName . "\n";
				if ($result->nodeName == $array[$k + 1])
					$flag = 1;
			}
			print "flag:". $flag . "\n";

			if (flag == 0)
			{	
				AppendNodeNoValue($array[$k], $array[$k + 1]);
			}
			#$judge = $results->item(0)->hasChildNodes();
			#if ($judge == FALSE)
			#	print "FALSE\n";
			#else
			#	print "TRUE\n";
		}
		$string = $string . "/";
	}
	#if ($results->item(0) == NULL)
	#	print "IS NULL\n";

	#$judge = $results->item(0)->hasChildNodes();
	
	#if ($judge == FALSE)
	#	print "FALSE\n";
	#else
	#	print "TRUE\n";
	#
	#print "Judge". $judge;

	#hasChildNodes();
	#print $results->item(0)->nodeName;
}

Judge_Xpath($query_string);

?>
